<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;

class IndexUserShop extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_user_shop';


    protected static function init()
    {
        //TODO:初始化内容
    }

    /**
     * 我的商品id
     */
    public function MyGoodList($dis){
        $data = self::where($dis)->column('goods_id');
        return !empty($data)?array_values($data):['9999999'];
    }

    /**
     * @param $dis
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取商城banner
     */

    public function getShopBanner($dis){
        $data = Db::name('longbing_card_shop_carousel')->where($dis)->order('top desc')->select()->toArray();
        $data = transImages($data,['img']);
        if(!empty($data)){
            foreach ($data as &$v){
                $v['cover'] = !empty($v['img'][0])?$v['img'][0]:'';
            }
        }
        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-11 16:09
     * @功能说明:商城轮播图
     */
    public function shopBannerList($dis,$file = '*'){

        $data = Db::name('longbing_card_shop_carousel')->where($dis)->field($file)->order('top desc')->select()->toArray();
        $data = transImages($data,['img']);
        if(!empty($data)){
            foreach ($data as &$v){

                $v['linkType'] = 4;

                $img = $v['img'];

                $v['img'] = [];

                $v['img'][0]['url'] = $img[0];
            }
        }
        return $data;

    }

    /**
     * @param $data
     * 添加到我到商品
     */
    public function userShopAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data = $this->insert($data);
        return $data;
    }

    /**
     * @param $dis
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取商城主推商品详情
     */
    public function userShopInfo($dis){
        $data = $this->where($dis)->find();
        return !empty($data)?$data->toArray():$data;
    }


    /**
     * @param $dis
     * @功能说明: 获取我的商品数量
     * @author chenniang
     * @DataTime: 2019-12-27 11:23
     */
    public function userShopCount($dis){
       $data= $this->alias('a')
             ->join('longbing_card_goods b','a.goods_id = b.id AND b.status = 1')
             ->where($dis)
             ->count();
       return $data;
    }





}